package com.antexpress.controller;

import com.antexpress.bean.Authentication;
import com.antexpress.common.Const;
import com.antexpress.common.Response;
import com.antexpress.pojo.Department;
import com.antexpress.pojo.Employee;
import com.antexpress.pojo.Title;
import com.antexpress.service.DepartmentService;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 *
 * @author Andy
 */
@Controller
@RequestMapping(value="/dept/")
public class DepartmentController {
    @Autowired
    private DepartmentService deptService;
        
    @RequestMapping(value="save_dept.do", method=RequestMethod.POST)
    @ResponseBody
    public Response addOrUpdateDept(String id, String name, String comment, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(emp!=null && Authentication.isHRAdmin(emp)){  //登录，且拥有HR管理员权限
            if(name==null||name.equals("")){
                resp = Response.createByErrorMessage("Name can't be blank!");
            }else {
                Department dept = new Department();
                dept.setName(name);
                dept.setComment(comment);
                if(id!=null&&!id.equalsIgnoreCase("")){
                    dept.setId(id);
                }
                resp = deptService.addOrUpdateDepartment(dept);
            }
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
    @RequestMapping(value="list_dept_details.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listDeptDetails(String[] deptIds, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(emp!=null && Authentication.isHRAdmin(emp)){  //登录，且拥有HR管理员权限
            List<String> deptIdList = Arrays.asList(deptIds);
            resp = deptService.getDeptDetailsByIds(deptIdList);
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
    @RequestMapping(value="list_dept.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listDept(HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(emp!=null && Authentication.isHRAdmin(emp)){  //登录，且拥有HR管理员权限
            resp = deptService.getAllDept();
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
    @RequestMapping(value="list_title.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listTitle(HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(emp!=null && Authentication.isHRAdmin(emp)){  //登录，且拥有HR管理员权限
            resp = deptService.getAllTitle();
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
    @RequestMapping(value="list_dept_title.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listDeptAndTitle(HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(emp!=null && Authentication.isHRAdmin(emp)){  //登录，且拥有HR管理员权限
            resp = deptService.getAllDeptAndTitle();
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
    @RequestMapping(value="get_all_authority.do", method=RequestMethod.POST)
    @ResponseBody
    public Response listAuthority(){
        return deptService.getAllAuthority();
    }
    
    @RequestMapping(value="save_title.do", method=RequestMethod.POST)
    @ResponseBody
    public Response addOrUpdateTitle(String id, String deptId, String name, String authority, String comment, HttpSession session) {
        Response resp = null;
        Employee emp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(emp!=null && Authentication.isHRAdmin(emp)){  //登录，且拥有HR管理员权限
            if(name==null||name.equals("")){
                resp = Response.createByErrorMessage("Name can't be blank!");
            }else if(deptId==null||deptId.equals("")){
                resp = Response.createByErrorMessage("Department can't be blank!");
            }else if(authority==null||authority.equals("")){
                resp = Response.createByErrorMessage("Authority can't be blank!");
            }else {
                Title title = new Title();
                Department dept = new Department();
                dept.setId(deptId);
                title.setDepartment(dept);
                title.setName(name);
                title.setAuthority(Integer.valueOf(authority));
                title.setComment(comment);
                if(id!=null&&!id.equalsIgnoreCase("")){
                    title.setId(id);
                }
                resp = deptService.addOrUpdateTitle(title);
            }
        }else {
            resp = Response.createByErrorMessage("Sorry, verifaction failed!");
        }
        return resp;
    }
    
    @RequestMapping(value="list_titles.do", method=RequestMethod.POST)
    @ResponseBody
    public Response getTitleByDeptId(String deptId){
        Response resp = null;
        if(deptId != null && !deptId.equals("")){
            resp = deptService.getTitlesByDeptId(deptId);
        }else{
            resp = Response.createByErrorMessage("Please input a valid department id!");
        }
        return resp;
    }
    
    @RequestMapping(value="delete_title.do", method=RequestMethod.POST)
    @ResponseBody
    public Response deleteTitleById(String titleId, HttpSession session){
        Response resp = null;
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(currentEmp==null)
            return Response.createByErrorMessage("Sorry, verifaction failed!");
        if(titleId==null||titleId.equals(""))
            return Response.createByErrorMessage("Please select a title!");
        if(!Authentication.isTitleAllowedDeleted(titleId))
            return Response.createByErrorMessage("The title you selected is basic title, it's not allowed to delete!");
        resp = deptService.deleteTitleById(titleId);
        return resp;
    }
    
    @RequestMapping(value="delete_dept.do", method=RequestMethod.POST)
    @ResponseBody
    public Response deleteDeptById(String deptId, HttpSession session){
        Response resp = null;
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(currentEmp==null)
            return Response.createByErrorMessage("Sorry, verifaction failed!");
        if(deptId==null||deptId.equals(""))
            return Response.createByErrorMessage("Please select a department!");
        if(!Authentication.isDeptAllowedDeleted(deptId))
            return Response.createByErrorMessage("The option you selected is basic department, it's not allowed to delete!");
        resp = deptService.deleteDeptById(deptId);
        return resp;
    }
    
    @RequestMapping(value="batch_delete_title.do", method=RequestMethod.POST)
    @ResponseBody
    public Response batchDeleteTitle(String[] titles, HttpSession session){
        Response resp = null;
        Employee currentEmp = (Employee) session.getAttribute(Const.CURRENT_USER);
        if(currentEmp==null)
            return Response.createByErrorMessage("Sorry, verifaction failed!");
        if(titles==null||titles.length==0)
            return Response.createByErrorMessage("Please select some titles!");
        for(String t:titles){
            if(!Authentication.isTitleAllowedDeleted(t))
                return Response.createByErrorMessage("The title you selected is basic title, it's not allowed to delete!");
        }
        resp = deptService.batchDeleteTitles(titles);
        return resp;
    }
}
